package com.gujiangbo.juc;

import oracle.jvm.hotspot.jfr.ThreadGroupEntry;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * @Author: gujiangbo.co
 * @Contact: 646832012@qq.com
 * @Date: 2022/6/2 10:07
 * @Version: 1.0
 * @Description:
 */
public class FutureThreadPoolTest {
    public static void main(String[] args) throws Exception {

        // 3个任务
        long startTime = System.currentTimeMillis();
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        FutureTask f1 = new FutureTask<>(() -> {
            try {
                TimeUnit.MILLISECONDS.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "task 1 over.";
        });
        FutureTask f2 = new FutureTask<>(() -> {
            try {
                TimeUnit.MILLISECONDS.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "task 2 over.";
        });
        FutureTask f3 = new FutureTask<>(() -> {
            try {
                TimeUnit.MILLISECONDS.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "task 3 over.";
        });
        executorService.submit(f1);
        executorService.submit(f2);
        executorService.submit(f3);

        System.out.println(f1.get());
        System.out.println(f2.get());
        System.out.println(f3.get());

        executorService.shutdown();
        long endTime = System.currentTimeMillis();
        System.out.println("-----costTime:" + (endTime - startTime) + " 毫秒");


    }

    private static void m1() {
        long startTime = System.currentTimeMillis();
        //暂停5秒钟线程
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //暂停10秒钟线程
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        long endTime = System.currentTimeMillis();
        System.out.println("-----costTime:" + (endTime - startTime) + " 毫秒");
        System.out.println(Thread.currentThread().getName() + "\t -----end ");
    }
}

